---
title: 
- Replication package for Karreth, Tir & Gibler (JPR)
subtitle:
- "Latent Territorial Threat and Democratic Regime Reversals"
author: 
- Johannes Karreth
- Jaroslav Tir
- Douglas M. Gibler
date: 
- January 25, 2021

output:
  rmdformats::readthedown:
    highlight: default
    smart: false
---

This folder contains all data and code to reproduce the analyses in:

> Karreth, Johannes, Jaroslav Tir, and Douglas M. Gibler. Forthcoming. "Latent Territorial Threat and Democratic Regime Reversals." *Journal of Peace Research*. [doi:10.1177/00223433211009770](https://doi.org/10.1177%2F00223433211009770)

This document makes reference to the supporting information (SI), which is part of this replication package and also posted at <http://www.jkarreth.net>. For any questions, please contact the corresponding author at <jkarreth@ursinus.edu>.

To get straight to the analyses presented in the article, please open and run the script `3_analyze_reversals.R`. To reproduce the data used in the analyses, please continue reading below. 

# Software requirements

Please install R (version 4.0 or higher) and JAGS (version 4.3) before proceeding. A list of R packages used in this project is at the end of this file.

Each script using Bayesian estimation sets seeds for random number generators. However, your analysis might still produce slightly different scores if you use a different version of rstanarm or if your rstanarm installation has otherwise different parameters. These deviations should not affect the subsequent estimation results in substance.

Most of the Bayesian models in this project take some time to estimate (at least on my Macbook Pro with 16GB RAM). Therefore, in all scripts I:

- commented out the calls to rstanarm and JAGS
- saved the fitted model objects in this replication package
- load the fitted model objects in the workspace for further processing (tables, figures, predicted probabilities, etc.)

If you wish to re-estimate the Bayesian models, simply un-comment the relevant code where indicated in the script files.


# Analysis 

The script `3_analyze_reversals.R` first imports the following datasets, which are part of this replication package:

- `Source/distpol.RData`
- `Source/neighbors.RData`
- `Source/polity.RData`
- `Source/econ.RData`
- `Source/HSIGOs/hsigo_cow30.csv_1945-2017.csv`
- `Source/mid4_3.csv`
- `Source/contiguity_rivalries_cy.csv`
- `Source/QoG/qog_std_ts_jan19_reduced.csv`
- `Work/TTpred.csv` (via `2_predict_TT.R`)

Next, the script performs the following tasks:

- compiling the data for analysis
- recoding some variables for analysis
- conducting the analyses in the article and supporting information (`ktg_SI.pdf`)

This script creates the following tables and figures:

- Figure 1
- Figure 2
- Table 1
- All tables and figures in the supporting information

All tables and figures produced by these scripts are written to a folder named "Output", which needs to be created before beginning the analyses. Note that in the article, "Model 1" refers to `m3` in this script, the model incorporating uncertainty from estimating the latent territorial threat scores. "Model 2" is `m2` in this script. 

The analysis script calls three separate R scripts (`m1_tojags.R`, `m2_tojags.R`, `m3_tojags.R`) to run parallel chains in JAGS. These R scripts refer to three JAGS models (`m1_jags.jags`, `m2_jags.jags`, `m3_jags.jags`). All of these files are also in the top folder of this replication package.

# Background: Data sets generated by the authors for this project

All raw datasets needed to create the data for analyses are provided within the same working directory as the R script.

## `Work/TTm.csv`

This dataset is used to estimate the latent territorial threat scores (Table A3). The file contains directed contiguous dyads. 

You can recreate this dataset using the script `1_generate_TTdyads.R`.

To run this script, the following original source datasets are provided in the replication package:

- `Source/DirectContiguity320/contdird.csv`
- `Source/MID 4.3/MIDB 4.3.csv`
- `Source/version4.1_comma/alliance_v4.1_by_dyad_yearly.csv`
- `Source/COW-war/Intra-StateWarData_v4.1.csv`
- `Source/NMC_5_0/NMC_5_0.csv`
- `Source/ICOW Colonial History 1.1/coldata110.csv`
- `Source/Territorial Change Data/Territorial Change Data, 1816-2018.xls`
- `Source/ICOW Provisional 1.01/ICOWprovyr101.csv`

## `Work/TTpred.csv`

This dataset contains latent territorial threat scores. 

You can recreate this dataset using the script `2_predict_TT.R`. 

To generate this dataset, the following files are provided in the replication package:

- `Work/TTm.csv`

# Data sources

Data sources are discussed and cited in the article. The files contained in this replication package for the purpose of executing all scripts are listed below. **Please note that these files are provided for replication purposes only. Use the original source to obtain the newest and authoritative version of each dataset.**

- `Source/mid4_3.csv`: COW Militarized Interstate Dispute Data, version 4.3 ([link](https://correlatesofwar.org/data-sets/MIDs/mid_level_4-3-1.zip/view))
- `Source/QoG/qog_std_ts_jan19_reduced.csv`: A reduced version of the Quality of Government Standard Dataset, version Jan19 ([link](https://www.gu.se/en/quality-government/qog-data/data-downloads/data-archive))
- `Source/DirectContiguity320/contdird.csv`: COW Direct Contiguity Data, version 3.20 ([link](https://correlatesofwar.org/data-sets/direct-contiguity))
- `Source/MID 4.3/MIDB 4.3.csv`: COW Militarized Interstate Dispute Data, version 4.3 ([link](https://correlatesofwar.org/data-sets/MIDs/mid_level_4-3-1.zip/view))
- `Source/version4.1_comma/alliance_v4.1_by_dyad_yearly.csv`: COW Formal Interstate Alliance Dataset, version 4.1 ([link](https://correlatesofwar.org/data-sets/formal-alliances))
- `Source/COW-war/Intra-StateWarData_v4.1.csv`: COW Intra-State War Dataset, version 4.1 ([link](https://correlatesofwar.org/data-sets/COW-war/intra-state-wars-v5-1.zip/view))
- `Source/NMC_5_0/NMC_5_0.csv`: COW National Material Capabilities Dataset, version 5.0 ([link](https://correlatesofwar.org/data-sets/national-material-capabilities))
- `Source/ICOW Colonial History 1.1/coldata110.csv`: ICOW Colonial History Dataset, version 1.1 ([link](http://www.paulhensel.org/icowcol.html))
- `Source/Territorial Change Data/Territorial Change Data, 1816-2018.xls`: COW Territorial Change Dataset, version 6.0 ([link](https://correlatesofwar.org/data-sets/territorial-change))
- `Source/ICOW Provisional 1.01/ICOWprovyr101.csv`: ICOW Territorial Claims Data, provisional version 1.01 ([link](http://www.paulhensel.org/icowterr.html))
- `Source/HSIGOs/hsigo_cow30.csv_1945-2017.csv`: Tir & Karreth membership counts in highly structured IGOs, updated version from [*Incentivizing Peace*](http://www.jkarreth.net/incentivizing-peace.html) using the COW IGO data, version 3.0
- `Source/Polity/p4v2018.xls`: Polity IV data, version 2018 ([link](http://www.systemicpeace.org/inscrdata.html))

# Software version information

All analyses using R were executed on 4.0.3 (2020-10-10) on macOS Big Sur 10.16; a list of packages and versions used is at the end of this file. 

Because the default settings for `read.csv()` changed with the introduction of R 4.0, please carefully check your R version if running into trouble with variable types. Problems related to this issue should be fixable by adding `stringsAsFactors = FALSE` to the `read.csv()` call.

```
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] dclone_2.3-0       Matrix_1.2-18      coda_0.19-4        knitr_1.30        
 [5] pastecs_1.3.21     cobalt_4.2.4       Matching_4.9-7     separationplot_1.3
 [9] foreign_0.8-80     MASS_7.3-53        Hmisc_4.4-2        Formula_1.2-4     
[13] survival_3.2-7     lattice_0.20-41    RColorBrewer_1.1-2 cowplot_1.1.0     
[17] BayesPostEst_0.2.1 rstanarm_2.21.1    Rcpp_1.0.6         xtable_1.8-4      
[21] countrycode_1.2.0  reshape_0.8.8      forcats_0.5.0      stringr_1.4.0     
[25] dplyr_1.0.2        purrr_0.3.4        readr_1.4.0        tidyr_1.1.2       
[29] tibble_3.0.4       ggplot2_3.3.2      tidyverse_1.3.0   

loaded via a namespace (and not attached):
  [1] utf8_1.1.4           tidyselect_1.1.0     lme4_1.1-26          htmlwidgets_1.5.3   
  [5] grid_4.0.3           miscTools_0.6-26     jtools_2.1.1         munsell_0.5.0       
  [9] codetools_0.2-16     statmod_1.4.35       DT_0.16              miniUI_0.1.1.1      
 [13] withr_2.3.0          colorspace_2.0-0     highr_0.8            rstudioapi_0.13     
 [17] ROCR_1.0-11          stats4_4.0.3         gbRd_0.4-11          bayesplot_1.7.2     
 [21] Rdpack_2.1           labeling_0.4.2       rstan_2.21.2         mnormt_2.0.2        
 [25] clarkeTest_0.1.0     farver_2.0.3         vctrs_0.3.6          generics_0.1.0      
 [29] xfun_0.20            R6_2.5.0             markdown_1.1         rmdformats_1.0.1    
 [33] VGAM_1.1-4           bitops_1.0-6         assertthat_0.2.1     promises_1.1.1      
 [37] scales_1.1.1         nnet_7.3-14          texreg_1.37.5        gtable_0.3.0        
 [41] processx_3.4.5       sandwich_3.0-0       rlang_0.4.10         splines_4.0.3       
 [45] checkmate_2.0.0      rjags_4-10           broom_0.7.3          inline_0.3.17       
 [49] yaml_2.2.1           reshape2_1.4.4       abind_1.4-5          modelr_0.1.8        
 [53] unmarked_1.0.1       threejs_0.3.3        crosstalk_1.1.0.1    backports_1.2.1     
 [57] httpuv_1.5.4         rsconnect_0.8.16     DAMisc_1.6.2         tools_4.0.3         
 [61] bookdown_0.21        psych_2.0.12         runjags_2.0.4-6      optiscale_1.2       
 [65] ellipsis_0.3.1       raster_3.4-5         R2WinBUGS_2.1-21     ggridges_0.5.2      
 [69] plyr_1.8.6           base64enc_0.1-3      ps_1.5.0             prettyunits_1.1.1   
 [73] rpart_4.1-15         zoo_1.8-8            cluster_2.1.0        haven_2.3.1         
 [77] fs_1.5.0             survey_4.0           magrittr_2.0.1       data.table_1.13.6   
 [81] openxlsx_4.2.3       colourpicker_1.1.0   lmtest_0.9-38        reprex_0.3.0        
 [85] effects_4.2-0        tmvnsim_1.0-2        matrixStats_0.57.0   hms_0.5.3           
 [89] shinyjs_2.0.0        mime_0.9             evaluate_0.14        shinystan_2.5.0     
 [93] rio_0.5.16           jpeg_0.1-8.1         readxl_1.3.1         gridExtra_2.3       
 [97] rstantools_2.1.1     compiler_4.0.3       bdsmatrix_1.3-4      V8_3.4.0            
[101] crayon_1.3.4         minqa_1.2.4          StanHeaders_2.21.0-7 htmltools_0.5.0     
[105] later_1.1.0.1        RcppParallel_5.0.2   lubridate_1.7.9.2    DBI_1.1.0           
[109] ggmcmc_1.5.0         dbplyr_2.0.0         boot_1.3-25          AICcmodavg_2.3-1    
[113] car_3.0-10           cli_2.2.0            mitools_2.4          rbibutils_2.0       
[117] insight_0.11.1       igraph_1.2.6         pkgconfig_2.0.3      sp_1.4-4            
[121] R2jags_0.6-1         xml2_1.3.2           dygraphs_1.1.1.6     plm_2.2-5           
[125] rvest_0.3.6          snakecase_0.11.0     callr_3.5.1          digest_0.6.27       
[129] janitor_2.0.1        rmarkdown_2.6        cellranger_1.1.0     htmlTable_2.1.0     
[133] maxLik_1.4-6         curl_4.3             shiny_1.5.0          gtools_3.8.2        
[137] nloptr_1.2.2.2       lifecycle_0.2.0      nlme_3.1-149         jsonlite_1.7.2      
[141] carData_3.0-4        fansi_0.4.1          pillar_1.4.7         GGally_2.0.0        
[145] loo_2.4.1            fastmap_1.0.1        httr_1.4.2           pkgbuild_1.2.0      
[149] glue_1.4.2           xts_0.12.1           zip_2.1.1            png_0.1-7           
[153] shinythemes_1.1.2    pander_0.6.3         stringi_1.5.3        caTools_1.18.0      
[157] latticeExtra_0.6-29 
```
